﻿package cn.poco.pepsi.activity.init;






import cn.poco.pepsi.R;
import cn.poco.pepsi.activity.tab.TabActivity;
import cn.poco.pepsi.constants.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

public class InitActivity extends Activity {
	
	//handler是成员变量，先初始化再执行onCreate方法
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 1){
				
				//记录当前城市名及代码
				SharedPreferences preferences  = getSharedPreferences(Constants.CITY_CODE, Context.MODE_PRIVATE);
				
				
				
				if(preferences.contains(Constants.CITY_CODE)){
					
					//指向跳至TabActivity界面（城市地区列表，带有Tab标签的）
					Intent tabInent  = new Intent(InitActivity.this,TabActivity.class);
				
					//加入城市名及代码
					Bundle bundle = new Bundle();
					bundle.putLong(Constants.CITY_CODE, preferences.getLong(Constants.CITY_CODE, 0));
					bundle.putString(Constants.CITY_NAME, preferences.getString(Constants.CITY_NAME, ""));
					System.out.println("what==1^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+preferences.getString(Constants.CITY_NAME, "空空空")+"&&"+preferences.getLong(Constants.CITY_CODE, 0));
					tabInent.putExtras(bundle);
					startActivity(tabInent);
				}else{
					System.out.println("SwitchCityActivity^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
					//指向跳至SwitchCityActivity界面（切换城市）
					Intent intent = new Intent(InitActivity.this,SwitchCityActivity.class);
					startActivity(intent);
				}
				
				
				/*CacheDbService db = new CacheDbService(InitActivity.this);
				int num = db.checkRecordNum();
				if(num>=500){
					deleteRecord(db);
				}*/
				
				//关闭本界面
				InitActivity.this.finish();
				handler.removeMessages(1);
			}
			
		}
	};
	
/*	//根据数据库记录删除缓存图片
	private void deleteRecord(CacheDbService db){
		ArrayList<String> paths = db.backDeleteRecord();
		File file;
		for(int i=0;i<paths.size();i++){
			file = new File(paths.get(i));
			file.delete();
		}
		db.deleteRecord();
	}*/

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//去掉标题、显示软件启动画面
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//init.xml布局文件，只有一张图片
		setContentView(R.layout.init);
		Constants.exit   = "start";
		
		//3秒后自动进入登陆界面
		handler.sendEmptyMessageDelayed(1, 3000);
		/*CacheDbService cacheDbService = new CacheDbService(this);
		ImageCache imageCache = ImageCache.getInstances();
		imageCache.setImageUris(cacheDbService.findAllUri());*/
	
	}

	
}
